package com.xgpro.mapper.talentintroduction;

import com.xgpro.domain.rolemanage.dto.UserDTO;
import com.xgpro.domain.rolemanage.entity.Role;
import com.xgpro.domain.studycentre.vo.TeacherBack;
import com.xgpro.domain.talentintroduction.dto.UserMessage;
import com.xgpro.domain.talentintroduction.entity.User;
import com.xgpro.domain.talentintroduction.vo.UserInfo;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;
import java.util.Map;

/**
 * @author tx
 * @version 1.0
 * @date 2019/8/20 20:49
 */
public interface UserMapper  extends Mapper<User>{
    /**
     * 根据id的list查询相关用户信息
     * @param ids
     * @return
     */
    @Select("<script>" +
            "select t_user.id as id,t_user.username as username,t_user.photo as photo,t_deptment.name as deptname from t_user left join t_deptment on t_user.dept_id=t_deptment.id" +
            "<where>" +
            "t_user.id in" +
            "<foreach collection='list' index='index' item='id'  open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</where>" +
            "</script>")
    public List<UserMessage> selectByList(List<Integer> ids);

    /**
     * 通过userid查询用户信息
     * @param userId
     * @return
     */
    @Select("<script>" +
            "select t_user.username as name,t_user.phone as phone,t_edu.edu_kind as edu,t_user.job as job,t_rank.name as rankName,t_user.job_time as jobTime " +
            "from t_user left join t_edu on t_user.edu_id=t_edu.id  left join t_rank on t_user.rank_id=t_rank.id " +
            "where t_user.id=#{userId}" +
            "</script>")
    public UserInfo selectById(Integer userId);

    /**
     * 导师的个人信息
     * @param id
     * @return
     */
    @Select("select t_user.id as userId,t_user.username as name,t_tea.job_title as jobTitle,t_tea.honor_title as honorTitle," +
            "t_tea.achievement as achievement,t_tea.tea_year as teaYear,t_user.photo as photo,t_deptment.name as deptName from t_user " +
            "left join t_deptment on t_user.dept_id=t_deptment.id left join t_tea on t_user.id=t_tea.user_id where t_user.id=#{id}")
    public TeacherBack selectTeacherByUserId(Integer id);

    /**
     * 通过用户id查询用户所有角色
     * @param userId
     * @return
     */
    @Select("<script>" +
            "SELECT " +
            "t1.id as id," +
            "t1.role_name as roleName," +
            "t1.description as description," +
            "t1.create_time as createTime " +
            "FROM " +
            "t_role t1 " +
            "JOIN t_user_role t2 ON t1.id = t2.role_id " +
            "WHERE " +
            "t2.user_id =#{userId}" +
            "</script>")
    List<Role> findRoleListById(Integer userId);

    @Insert("<script>" +
            "insert into t_user_role" +
            "(user_id,role_id) " +
            "values(#{userId},#{roleId}) " +
            "</script>")
    int assignRole(@Param("userId") Integer userId,@Param("roleId") Integer roleId);

    /**
     * 查询外部导师人员信息
     * @param name
     * @return
     */
    @Select("<script>" +
            "select " +
            "u.username ,u.id from t_tea AS t " +
            "LEFT JOIN t_user AS u ON u.id=t.user_id and t.tea_type = 1 " +
            "where " +
            "u.username like concat('%',#{name},'%') " +
            "GROUP BY u.username " +
            "</script>")
    List<User> selectOutTeacher(String name);


    @Delete("<script>" +
            " delete from t_user where  id in " +
            " <foreach collection='list' index='index' item='id'  open='(' separator=',' close=')'> " +
            "  #{id}" +
            " </foreach>" +
            " </script>")
    Integer batchDeletes(List<Integer> ids);

    @Delete("<script>" +
            " delete from t_user_role where  user_id = #{userId} " +
            " </script>")
    Integer deleteUserRoleByUserId(Integer userId);

    @Select("<script>" +
            " SELECT " +
            " t1.id,  " +
            " t1.user_id AS userId,  " +
            " t1.username,  " +
            " t1.photo,  " +
            " t1.role_id as roleId,  " +
            " t1.mail,  " +
            " t1.phone,  " +
            " t1.dept_id as  deptId,  " +
            " t1.job ,  " +
            " t1.edu_id AS eduId,  " +
            " t1.rank_id AS rankId,  " +
            " t1.rank_update as rankUpdate,  " +
            " t1.up_flag as  upFlag,  " +
            " t1.tea_status AS teaStatus,  " +
            " t1.job_time AS jobTime,  " +
            " t1.job_title AS jobTitle,  " +
            " t1.queue,  " +
            " t1.hired_date AS hiredDate,  " +
            " t1.sex_type AS sexType,  " +
            " t1.cert_no AS certNo,  " +
            " t1.birthday,  " +
            " t1.second_dept_id AS secondDeptId,  " +
            " t3.name,  " +
            " IFNULL(t4.edu_kind,-1) as eduKind  " +
            " FROM  " +
            " t_deptment t3,  " +
            " t_user t1  " +
            " LEFT JOIN t_edu t4 ON t1.edu_id = t4.id   " +
            " WHERE  t1.dept_id IS NOT NULL " +
            " AND t1.dept_id = t3.id  " +
            " AND t1.username not like '%(已离职)%'  " +
            " <if test=\"name != null and name !='' \"> " +
            "    and  t1.username LIKE  CONCAT('%',#{name},'%')" +
            " </if>" +
            " <if test=\"deptId != null and deptId !='' \"> " +
            " AND t1.dept_id = #{deptId} " +
            " </if>" +
            " <if test=\"education != null and education !='' \"> " +
            " AND EXISTS ( SELECT 1 FROM t_edu t2 WHERE t2.id = t1.edu_id and  t2.edu_kind = #{education} )   " +
            " </if>" +
            " <if test=\"userId != null and userId !='' \"> " +
            " AND t1.user_id = #{userId} " +
            " </if>" +
            " <if test=\"role != null and role !='' \"> " +
            " AND EXISTS ( select 1 from t_user_role where  user_id = t1.id and  role_id =#{role})  " +
            " </if>" +
            " </script>")
    List<UserDTO> findListBycondition(Map<String,Object> param);


    @Select("<script>" +
            " SELECT " +
            " id,  " +
            " user_id as userId  " +
            " FROM t_user " +
            " WHERE 1=1 " +
            " <if test=\"username != null and username !='' \"> " +
            "  AND   username = #{username}" +
            " </if>" +
            " <if test=\"userId != null and userId !='' \"> " +
            " AND user_id = #{userId} " +
            " </if>" +
            " LIMIT 1 " +
            " </script>")
    User getUser(@Param("username")String username,@Param("userId")String userId);

    /**
     * 查询所有公司管理员
     * @param
     * @return
     */
    @Select("select t_user.id  from t_user where t_user.id in (select user_id from t_user_role where role_id=1)")
    List<Integer> selectListByRoleId();
}
